package com.srpingde.hhh;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.FenLeiData;
import com.srpingde.hhh.bean.FenLeiDetailBean;
import com.srpingde.hhh.bean.FenLeiDetailDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class FenLeiNameActivity extends Activity {
	ImageView back;
	TextView top_title;
	FenLeiData fenLeiData;
	HHHIml weiQuIml;
	GridView fenlei_name_gridview;
	String order = "1";
	//RadioGroup group;
	FenLeiDetailBean fenLeiDetailBean;
	boolean ischeck;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fen_lei_name);
        fenlei_name_gridview = (GridView) findViewById(R.id.fenlei_name_gridview);
       // group = (RadioGroup) findViewById(R.id.group_fenlei_name);
        layout();
//        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				// TODO Auto-generated method stub
//				switch (checkedId) {
//				case R.id.remen:
//					getData("ishot",false);
//					break;
//				case R.id.xinpin:
//					getData("isnew",false);
//					break;
//				case R.id.gaodaodi:
//					if(ischeck){
//						ischeck = false;
//					}else{
//						ischeck = true;
//					}
//					Log.e("mytag", ischeck+"========判断");
//					getData("ishot",true);
//					break;
//				}
//			}
//		});
        fenlei_name_gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent in= new Intent(FenLeiNameActivity.this,XiangQingActivity.class);
				in.putExtra("id", fenLeiDetailBean.getRows().get(arg2).getPid());
				startActivity(in);
			}
		});
    }
    public void onClick(View v){
    	switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.remen:
			getData("ishot",false);
			break;
		case R.id.xinpin:
			getData("isnew",false);
			break;
		case R.id.gaodaodi:
			if(ischeck){
				ischeck = false;
			}else{
				ischeck = true;
			}
			Log.e("mytag", ischeck+"========判断");
			getData("ishot",true);
			break;
		}
    }
    private void layout() {
    	back = (ImageView) findViewById(R.id.back);
    	weiQuIml = new HHHIml();
    	if(getIntent().getExtras()!=null){
    		fenLeiData =  (FenLeiData) getIntent().getSerializableExtra("fenlei");
    		top_title = (TextView) findViewById(R.id.top_title);
    		top_title.setText("["+fenLeiData.getName()+"]");
    		getData("ishot",false);
    		Log.e("mytag", fenLeiData.getCid()+"==========得到的cid");
    	}
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
		
//		radioButton((RadioButton) findViewById(R.id.remen));
//		radioButton((RadioButton) findViewById(R.id.xinpin));
//		radioButton((RadioButton) findViewById(R.id.gaodaodi));
	}
    private void radioButton(RadioButton button){
    	RadioGroup.LayoutParams layoutParams = (android.widget.RadioGroup.LayoutParams) button.getLayoutParams();
    	layoutParams.width = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]/3)-20;
    	button.setLayoutParams(layoutParams);
    }
    private void getData(String condition,boolean check){
    	String str;
    	if(check){
    		if(ischeck){
    			str = HHHInterface.httpHeader + "api.php?tab=seachproducts&act=data&pagesize=100&page=1&cid="+fenLeiData.getCid()+"&sort=d";
    		}else{
    			str = HHHInterface.httpHeader + "api.php?tab=seachproducts&act=data&pagesize=100&page=1&cid="+fenLeiData.getCid()+"&sort=a";
    		}
    	}else{
    		 str = HHHInterface.httpHeader + "api.php?tab=seachproducts&act=data&pagesize=100&page=1&cid="+fenLeiData.getCid()+"&condition="+condition;
    	}
    	weiQuIml.fenLeiList(str,FenLeiNameActivity.this,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag",content+"=======分类的数据=");
				fenLeiDetailBean = HHHDataManage.getInstance().getFenLeiDetailBean();
				fenlei_name_gridview.setAdapter(new FenLeiListViewAdapter(FenLeiNameActivity.this,fenLeiDetailBean.getRows()));
			}
		});
    }
    class FenLeiListViewAdapter extends BaseAdapter {
		List<FenLeiDetailDataBean> listBeans;
		Context context;
		public FenLeiListViewAdapter(Context context,List<FenLeiDetailDataBean> listBeans) {
			super();
			this.context = context;
			this.listBeans = listBeans;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listBeans.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			fenLeiHolder holder = null;
			if(arg1 == null){
				holder = new fenLeiHolder();
				arg1 = LayoutInflater.from(context).inflate(R.layout.fenlei_item_adapter, null);
				holder.icon = (ImageView) arg1.findViewById(R.id.fenlei_item_icon);
				arg1.setTag(holder);
			}else{
				holder = (fenLeiHolder) arg1.getTag();
			}
			itemLayout(holder.icon);
			//holder.icon.setBackgroundResource(images[arg0]);
			final ImageView kvShow = holder.icon ;
    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+listBeans.get(arg0).getUrl(), new SimpleImageLoadingListener(){
    			@Override
    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    				//kvShow.setImageBitmap(bitmap);
    				kvShow.setBackgroundDrawable(bitmapDrawable);
    			}
    		});	
			return arg1;
		}
		class fenLeiHolder{
			ImageView icon;
		}
	}
    private void itemLayout(ImageView imageView){
		int wh = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]/2);
		RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
		layoutParams.height = wh;
		layoutParams.width = wh;
		imageView.setLayoutParams(layoutParams);
	}
}
